package cn.hss.Collection;

import cn.hss.object.stat.A;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
/**ArrayList和LinkedList插入数据性能比较分析
 * @author 作者：HeShaoShuai
 * @Email 邮箱：2569819541@qq.com
 * @Date 日期：2023/4/27 9:05
 * @Package 包名：cn.hss.Collection
 * @Project 项目名：JavaSE
 * @Version 1.0
 */
public class InsertDate {
	public static void main(String[] args) {
		//在头部插入数据
		//headInsert();
		//创建ArrayList集合
		ArrayList<Integer> arrayList = new ArrayList<>();
		//创建LinkedList
		LinkedList<Integer> linkedList = new LinkedList<>();
		for(int i=0;i<449999;i++){
			arrayList.add(i);
			linkedList.add(i);
		}
		//使用迭代器遍历集合
		Iterator<Integer> it = arrayList.iterator();
		while(it.hasNext()){
			System.out.println(it.next());
		}
		//在尾部插入数据
		//tailInsert(arrayList,linkedList);
		//tailInsert();
		//在列表中间位置插入数据
		// centreInsert();
	}
	//在列表中间位置插入数据
	private static void centreInsert() {
		//创建ArrayList集合
		ArrayList<Integer> arrayList = new ArrayList<>();
		//创建LinkedList
		LinkedList<Integer> linkedList = new LinkedList<>();
		System.out.println("ArrayList中间位置插入数据");
		long l = System.currentTimeMillis();
		for(int i=0;i<450000;i++){
			int b=i>>1;
			arrayList.add(b, i);
		}
		long l1 = System.currentTimeMillis()-l;
		System.out.println("ArrayList中间位置插入数据耗时："+l1);
		System.out.println("LinkedList中间位置插入数据");
		long l2 = System.currentTimeMillis();
		for(int i=0;i<450000;i++){
			int b=i>>1;
			linkedList.add(b, i);
		}
		long l3 = System.currentTimeMillis()-l2;
		System.out.println("LinkedList中间位置插入数据耗时："+l3);
	}

	//在集合尾部插入数据
	private static void tailInsert(
			ArrayList<Integer> arrayList,
			LinkedList<Integer> linkedList) {
		System.out.println("LinkedList集合尾部插入数据");
		long l2 = System.currentTimeMillis();

		for(int i=0;i<450000;i++){
			linkedList.addLast(520);
		}
		long l3 = System.currentTimeMillis()-l2;
		System.out.println("LinkedList集合最后一个元素："+linkedList.getLast());
		System.out.println("ArrayList尾部插入数据");
		long l = System.currentTimeMillis();
		for(int i=0;i<450000;i++){
			arrayList.add(arrayList.size(), 520);
		}
		long l1 = System.currentTimeMillis()-l;
		//System.out.println(arrayList.size());
//   System.out.println("ArrayList最后一个元素："+arrayList.get(arrayList.size()));
		System.out.println("ArrayList尾部插入数据耗时："+l1);
	}

	private static void tailInsert() {
		ArrayList<Integer> arrayList = new ArrayList<>();
//        //创建LinkedList
		LinkedList<Integer> linkedList = new LinkedList<>();
		System.out.println("集合尾部插入数据");
		long l = System.currentTimeMillis();
		for(int i=0;i<450000;i++){
			linkedList.addLast(i);
		}
		long l1 = System.currentTimeMillis()-l;
		System.out.println("LinkedList尾部插入数据耗时："+l1);
		System.out.println("------------");
		long l2=System.currentTimeMillis();
		for(int i=0;i<450000;i++){
			//arrayList.add(i);
			arrayList.add(arrayList.size()-1, 520);
		}
		long l3 = System.currentTimeMillis()-l2;
		System.out.println("ArrayList尾部插入数据耗时："+l3);
	}


	//在集合头部插入数据
	private static void headInsert() {
		//创建ArrayList集合
		ArrayList<Integer> arrayList = new ArrayList<>();
		//创建LinkedList
		LinkedList<Integer> linkedList = new LinkedList<>();
		System.out.println("集合头部插入数据");
		long begin = System.currentTimeMillis();
		for(int i=0;i<450000;i++){
			//arrayList.add(i);
			arrayList.add(0, i);
		}
		long end = System.currentTimeMillis()-begin;
		System.out.println("ArrayList头部插入数据耗时："+end);
		long begin1 = System.currentTimeMillis();
		for (int i=0;i<450000;i++){
			linkedList.addFirst(i);
		}
		long end1 = System.currentTimeMillis()-begin1;
		System.out.println("LinkedList头部插入数据耗时："+end1);
	}
}